/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on 1 juin 2011, 14:35:31
 */

package cannes.gui;

import cannes.bean.planning.Film;
import cannes.bean.planning.Projection;
import cannes.bean.planning.Salle;
import cannes.dao.DAO;
import cannes.dao.DAOFactory;
import cannes.dao.implement.FilmDAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author p1008977
 */
public class NouvelleProjection extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NouvelleProjection() {
        initComponents();
        
        initLists();
        
    }
    
    private void initLists(){
        
        listFilm.setModel(listModelFilm);
        filmDAO = DAOFactory.getFilmDAO();

        listModelFilm.clear();
        listeFilms = filmDAO.findAll();
        for (Film film : listeFilms) {
            listModelFilm.addElement(film.getTitre());
        }
        
        listSalle.setModel(listModelSalle);
        salleDAO = DAOFactory.getSalleDAO();

        listModelSalle.clear();
        listeSalles = salleDAO.findAll();
        for (Salle salle : listeSalles) {
            listModelSalle.addElement(salle.getNom());
        }
        
        
        listDate.setModel(listModelDate);
        listModelDate.clear();
        String[] listeDates = { "09-05-2011","10-05-2011","11-05-2011","12-05-2011",
                                "13-05-2011","14-05-2011","15-05-2011","16-05-2011" };
        for (String date : listeDates) {
            listModelDate.addElement(date);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        labelFilm = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listFilm = new javax.swing.JList();
        labelSalle = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listSalle = new javax.swing.JList();
        labelDate = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        listDate = new javax.swing.JList();
        buttonOk = new javax.swing.JButton();
        buttonReset = new javax.swing.JButton();

        jButton1.setText("jButton1");

        labelFilm.setText("Film");

        listFilm.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listFilm.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listFilmValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listFilm);

        labelSalle.setText("Salle");

        listSalle.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listSalle.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listSalleValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(listSalle);

        labelDate.setText("Date");

        listDate.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(listDate);

        buttonOk.setText("OK");
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        buttonReset.setText("Reset");
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(labelFilm))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                            .addComponent(labelSalle, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                            .addComponent(labelDate)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonOk)
                        .addGap(18, 18, 18)
                        .addComponent(buttonReset)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFilm)
                    .addComponent(labelSalle)
                    .addComponent(labelDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOk)
                    .addComponent(buttonReset))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void listFilmValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listFilmValueChanged
        
        if(listFilm.isSelectionEmpty())
            return;
        //restriction de la liste des salles en fonction de la catégorie du film
        int index = listFilm.getSelectedIndex();
        Film f = listeFilms.get(index);
        String cat = f.getCategorie();
        listeSalles.clear();
        if(cat.equalsIgnoreCase("LM")){
            //1 : id de la salle des LM
            listeSalles.add(salleDAO.find(1));
            // 4 : salle des LM pour les lendemains
            listeSalles.add(salleDAO.find(4));
        }else if(cat.equalsIgnoreCase("CM")){
            // 2 : sélection UCR et CM 
            listeSalles.add(salleDAO.find(2));
            // 3 : sélection CM
            listeSalles.add(salleDAO.find(3));
        }else if(cat.equalsIgnoreCase("UCR")){
            // 2 : sélection UCR et CM 
            listeSalles.add(salleDAO.find(2));
            // 5 : sélection UCR du lendemain
            listeSalles.add(salleDAO.find(5));
        }
        
        //mise à jour de la liste
        listModelSalle.clear();
        for (Salle salle : listeSalles) {
            listModelSalle.addElement(salle.getNom());
        }
    }//GEN-LAST:event_listFilmValueChanged

    private void listSalleValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listSalleValueChanged
        if(listSalle.isSelectionEmpty())
            return;
        
        //ne rien faire si un film est déja sélectionné
        if(!listFilm.isSelectionEmpty())
            return;
        //restriction de la liste des salles en fonction de la catégorie du film
        int index = listSalle.getSelectedIndex();
        System.out.println("Index: "+index);
        listeFilms.clear();
        switch(listeSalles.get(index).getId()){
            //ID du theatre lumiere 
            case 1:
                System.out.println(listeSalles.get(index).getId());
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("LM"));
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("HC"));
                break;
                // ID de la salle debussy
            case 2:
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("UCR"));
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("CM"));
                break;
                //ID de la salle bunuel
            case 3:
                
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("CM"));
                break;
                // ID de la salle du soixantieme
            case 4:
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("LM"));                
                break;
                //ID de la salle Bazin
            case 5:
                listeFilms.addAll(((FilmDAO) filmDAO).findByCategorie("UCR"));
                break;
        }
        
        
        //mise à jour de la liste
        listModelFilm.clear();
        for (Film film : listeFilms) {
            listModelFilm.addElement(film.getTitre());
        }
        
    }//GEN-LAST:event_listSalleValueChanged

    private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
       initLists();
    }//GEN-LAST:event_buttonResetActionPerformed

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        proj = new Projection();
        //todo setId;
        int index = listFilm.getSelectedIndex();
        proj.setFilm(listeFilms.get(index).getId());
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        index = listDate.getSelectedIndex();
        Date d = new Date();
        try {
            d = sdf.parse((String) listDate.getSelectedValue());
        } catch (ParseException ex) {
            Logger.getLogger(NouvelleProjection.class.getName()).log(Level.SEVERE, null, ex);
        }
        proj.setHorraire(d);
        //TODO: changer l'id du planning en fonction de la catégorie
        proj.setPlanning(1);
        index = listSalle.getSelectedIndex();
        proj.setSalle(listeFilms.get(index).getId());
        //TODO: fenêtre de choix de l'heure
        ChoixDeHorraire cdh = new ChoixDeHorraire(proj);
        JFrame f = new JFrame("Choix Horraire");
        f.add(cdh);
        f.setSize(600, 600);
        cdh.setVisible(true);
        f.setVisible(true);
    }//GEN-LAST:event_buttonOkActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonOk;
    private javax.swing.JButton buttonReset;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelDate;
    private javax.swing.JLabel labelFilm;
    private javax.swing.JLabel labelSalle;
    private javax.swing.JList listDate;
    private javax.swing.JList listFilm;
    private javax.swing.JList listSalle;
    // End of variables declaration//GEN-END:variables
    javax.swing.DefaultListModel listModelFilm = new javax.swing.DefaultListModel();
    javax.swing.DefaultListModel listModelSalle = new javax.swing.DefaultListModel();
    javax.swing.DefaultListModel listModelDate = new javax.swing.DefaultListModel();
    
    DAO<Film> filmDAO;
    ArrayList<Film> listeFilms;
    
    DAO<Salle> salleDAO;
    ArrayList<Salle> listeSalles;
    
    Projection proj;
}
